<?php
/**
 * 	合同管理模板
 *  author： chengda
 *  date  ： 2017-11-22
 */
namespace Admin\Controller;

class ContractTemplateController extends AbstractController  {

	protected $From;

	public function _initialize(){
		$this->Form = D('contracttemplate');
    }
	

    public function index(){
	    $res = $this->Form->MyshowList();
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
        $this->display();
    }


    public function save($type = false, $id = 0){
    	
			$this->assign("data", $this->Form->GetCatalog());
			$this->display();
		
    }

    public function edit(){
    	if(I('ID')){
					$res = $this->Form->Myshow(array("ID" => (int)I('ID')));
					$this->assign("editdata",$res[0]);
					$this->assign("data", $this->Form->GetCatalog());
				}
		$this->display("save");
    }

    public function add(){
		$_POST['PubTime']=date('y-m-d h:i:s',time());
		if(!$this->Form->create()){
			exit($this->error($this->Form->getError()));
		}else{
			$result = $this->Form->add();
			if($result){
				$this->success('添加成功！');
			}else{
				$this->error('添加错误！');
			}
		}
    }

    public function delete(){
    	$res = $this->Form->MydeletP();
    	if($res['state']){
			$this->success($res['msg']);
		}else{
			$this->error($res['msg']);
		}
    }

    public function update(){
		$result = $this->Form->Myupdata();
		
		if($result['status'] == 1 && !empty($result)){
			$this->success($result['msg']);
		}else if($result['status'] == 0 && !empty($result)){
			$this->error($result['msg']);
		}
    }
    //预览
    function templatec($type=''){
    	$data=$this->Form->templatec();
        
    	if($type=='select'){
			$tpdata=$this->Form->rederTP();
			foreach ($tpdata as $key => $value) {
			  $aa=$value['id'];
			  $$aa=$value['value'];
			}
			$Content=$data['Content'];
        	eval("\$Content = \"$Content\";");
        	$this->assign("Title",$data['Title']);
    		$this->assign("Content",$Content);

    		$tp['ID']=$data['ID'];
    		$tp['Title']=$data['Title'];
    		$tp['Content']=htmlspecialchars_decode($Content);
    		$this->success($tp,'请求成功！','1');
    	}else{
    		$Content=$data['Content'];
        	//eval("\$Content = \"$Content\";");
        	$this->assign("Title",$data['Title']);
    		$this->assign("Content",$Content);
    		$this->display();
    	}
    }




    //pdf 打水印预览
    public function pdf($ID){
		$this->Form->pdf($ID);	
    }


    /**
     * 上传图片方法
     */
    function markupload(){

    	if($_FILES){
    		$rtn = "";
    		$config = array(
    			'maxSize' => 5*1024*1024,
    			'rootPath' => './data/upload/ContractTemplate/mark/',
    			'savePath' => '',
    			'saveName' => array('uniqid',''),
    			'exts' => array('jpg', 'gif', 'png', 'jpeg'),
    			'autoSub' => true,
    			'subName' => array('date','Ymd')
    		);
    		$this->mkdirs($config['rootPath']);
	    	$upload = new \Think\Upload($config);// 实例化上传类
	    	// 上传文件
	    	$info = $upload->upload($_FILES);
	    	if(!$info) {// 上传错误提示错误信息
	    		$rtn = $this->error($upload->getError());
	    	}else{// 上传成功
	    		foreach ($info as $key=>$img){
	    			if($key==0){
	    				$rtn .= $config['rootPath'].$img['savepath'].$img['savename'];
	    			}else{
	    				$rtn .= "|".$config['rootPath'].$img['savepath'].$img['savename'];
	    			}
	    		}
	    	}
	    	echo $rtn;
    	}

    }


	//选择模板
	public function Select($type=''){

		$res = $this->Form->MyshowList();
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
        $this->display();
	
	}

	// 获取模板关键词
	public function tplist(){
		$res =$this->Form->getTpList();
		echo json_encode($res,true); exit();
	} 



}